package com.example.shushiyunsst.fragment.main.home.child.miner22

import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.widget.Toolbar
import com.alibaba.fastjson.JSON
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.networking.RestClient
import com.example.library_common.utils.even.MessageWrap
import com.example.library_common.utils.even.TO_REFRESH_LIST
import com.example.shushiyunsst.R
import com.example.wallet.db.BalanceInfoManager
import com.example.wallet.db.UserInfoManager
import com.example.wallet.global.AppGlobals
import com.example.wallet.utils.selectorIcon
import com.senon.modularapp.util.JssSpannableString
import org.greenrobot.eventbus.EventBus

/**
 * 创建子账户
 * */
class CreateSubAccountFragment : BaseFragmentEx() {

    companion object {
        fun newInstance(): CreateSubAccountFragment {
            val args = Bundle()
            val fragment = CreateSubAccountFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private var moneyType = AppGlobals.getApplication().getString(R.string.wallet_cml)

    private lateinit var feedingTv: TextView

    private lateinit var mCostTv: TextView

    override fun getLayoutId(): Any {
        return R.layout.create_sub_account_fragment
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        moneyType= selectorIcon()
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val mToolbar: Toolbar = view.findViewById(R.id.mToolbar)
        mToolbar.title = "创建子账户"
        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }

        mCostTv = view.findViewById(R.id.mCostTv)
        feedingTv = view.findViewById(R.id.feedingTv)
        val feeBeanBean = BalanceInfoManager.mBalanceInfoManager.getFeeBeanBean()
        val minerFees = feeBeanBean?.addChildWalletAddrFee
        val spn = JssSpannableString(
            _mActivity,
            "矿工费: $minerFees $moneyType"
        )
            .first(minerFees.toString()).textColor(R.color.PrimaryColor)


        feedingTv.text = spn

        val mCost=feeBeanBean?.addChildWalletAddrCost?:""
        val mCostSpn = JssSpannableString(
            _mActivity,
            "服务费: $mCost $moneyType"
        )
            .first(mCost).textColor(R.color.PrimaryColor)

        mCostTv.text=mCostSpn

        feedingTv

        val sys = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo?.sysWalletAddress
        val accountTv: TextView = view.findViewById(R.id.accountTv)
        accountTv.text = sys

        val walletName: EditText = view.findViewById(R.id.walletName)

        view.findViewById<View>(R.id.createAccount).setOnClickListener {
            val walletNameStr = walletName.text.toString()
            if (walletNameStr.isNotEmpty()) {
                createAccount(walletNameStr)
            } else {
                Toast.makeText(_mActivity, "账户名称不能为空", Toast.LENGTH_SHORT).show()
            }
        }
    }

    private fun createAccount(walletNameStr: String) {
        val uid = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo?.uid
        val map = mapOf(
            "accountName" to walletNameStr,
            "uid" to uid
        )
        RestClient.builder()
            .url(R.string.createChindAcc)
            .raw(JSON.toJSONString(map))
            .success {
                Toast.makeText(_mActivity, "创建成功", Toast.LENGTH_SHORT).show()
                EventBus.getDefault().post(MessageWrap.getInstance(TO_REFRESH_LIST))
                onBackPressedSupport()
            }
            .failure {
                Toast.makeText(_mActivity, "发送失败", Toast.LENGTH_SHORT).show()
            }
            .error { _, msg ->
                Toast.makeText(_mActivity, msg, Toast.LENGTH_SHORT).show()
            }.build()
            .post()
    }
}